package DroidEye.DAO;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import DroidEye.HelloApplication;
import DroidEye.DAO.DAOInterface.AdjuDAOInterface;
import DroidEye.Dto.Adjudicator;
import DroidEye.Dto.Judge;
import DroidEye.JavaBean.AdjuBean;
import DroidEye.JavaBean.JudgeBean;
import DroidEye.Mapper.AdjudicatorMapper;
import DroidEye.Mapper.JudgeMapper;

@Service
public class AdjuDAO implements AdjuDAOInterface{

    @Autowired
    private AdjudicatorMapper adjudicatorMapper;
    
    @Autowired
    private JudgeMapper judgeMapper;

    @Override
    public AdjuBean queryAdjuAccount(String adjuID) {
        Adjudicator adjudicator = adjudicatorMapper.queryAdjuAccount(adjuID);
        if(adjudicator == null) {
            return null;
        }
        return custAdjudicator(adjudicator);
    }

    @Override
    public List<AdjuBean> queryAllAdju() {
        List<AdjuBean> adjuList = new ArrayList<>();
        List<Adjudicator> adjudicatorList = adjudicatorMapper.queryAllAdju();
        if(CollectionUtils.isEmpty(adjudicatorList)) {
            return adjuList;
        }
        return custAdjudicatorList(adjudicatorList);
    }

    @Override
    public boolean changeAdjuPassword(String adjuID, String newAdjuPassword) {
        Adjudicator adjudicator = new Adjudicator();
        adjudicator.setAdjudicatorId(adjuID);
        adjudicator.setAdjudicatorPassword(newAdjuPassword);
        int result = adjudicatorMapper.changeAdjuPassword(adjudicator);
        if(result > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<JudgeBean> queryAllNeededAdjuProject(String adjuID) {
        List<JudgeBean> judgeList = new ArrayList<>();
        List<Judge> judges = judgeMapper.queryAllNeededAdjuProject(adjuID);
        if(CollectionUtils.isEmpty(judges)) {
            return judgeList;
        }
        return custJudgeList(judges);
    }
    
    // 转换Judge为JudgeBean
    private JudgeBean custJudge(Judge judge) {
        JudgeBean judgeBean = new JudgeBean();
        judgeBean.setAdjuID(judge.getAdjuId());
        judgeBean.setStudentID(judge.getStudentId());
        judgeBean.setProjectID(judge.getProjectId());
        judgeBean.setProjectScore(judge.getProjectScore());
        return judgeBean;
    }
    
    // 转换JudgeList为JudgeBeanList
    private List<JudgeBean> custJudgeList(List<Judge> judges) {
        List<JudgeBean> judgeList = new ArrayList<>();
        for(Judge judge : judges) {
            JudgeBean judgeBean = custJudge(judge);
            judgeList.add(judgeBean);
        }
        return judgeList;
    }
    
    // 转换Adjudicator为AdjuBean
    private AdjuBean custAdjudicator(Adjudicator adjudicator) {
        AdjuBean adjuBean = new AdjuBean();
        adjuBean.setAdjuID(adjudicator.getAdjudicatorId());
        adjuBean.setAdjuPassword(adjudicator.getAdjudicatorPassword());
        adjuBean.setAdjuName(adjudicator.getAdjudicatorName());
        return adjuBean;
    }
    
    // 转换adjudicatorList为AdjuBeanList
    private List<AdjuBean> custAdjudicatorList(List<Adjudicator> adjudicatorList) {
        List<AdjuBean> adjuBeanList = new ArrayList<>();
        for(Adjudicator adjudicator : adjudicatorList) {
            AdjuBean adjuBean = custAdjudicator(adjudicator);
            adjuBeanList.add(adjuBean);
        }
        return adjuBeanList;
    }
    
    public static AdjuDAO getAdjuDAOInstance() {
        return HelloApplication.getBean("adjuDAO", AdjuDAO.class);
    }
}
